package com.atguigu.day21.bytestream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

/*
IO: input output 输入和输出
分类:
    流向分:
         输入和输出
    数据分:
        字节和字符
    角色分:
        节点和处理
四大基流 (抽象类):
   字节流
       InputStream
       OutputStream
   字符流:
       Reader
       Writer

字节输出流:
    以字节为单位写出数据
       字节输出流对象.write(数字)
       字节输出流对象.write(字节数组)
字节输入流:以字节为单位读取数据
     read() 一次读取一个字节 没有数据返回-1
     read(byte[]) 一次读取一个字节数组 没有数据返回-1



 */
public class InputStreamTest {
    @Test
    public void test05() throws Exception{
        //1.创建流对象
        InputStream is = new FileInputStream("D:/a.txt");
        //2.循环读取
        byte[] bArr = new byte[6];

        int length = 0;
        while ((length=is.read(bArr))!=-1){
            String s = new String(bArr, 0, length);
            System.out.println("s = " + s);
        }

        //3.关闭资源
        is.close();
    }


    @Test
    public void test04() throws Exception {
        //1.创建流对象
        InputStream is = new FileInputStream("D:/b.txt");
        //2.读取数据并展示
        byte[]bArr = new byte[3];
        System.out.println("读取前 = " + Arrays.toString(bArr));
        int length = is.read(bArr);
        System.out.println("1 读取后 = " + Arrays.toString(bArr)+" length = "+length+" , "+new String(bArr));
        length = is.read(bArr);
        System.out.println("2 读取后 = " + Arrays.toString(bArr)+" length = "+length+" , "+new String(bArr));
        length = is.read(bArr);
        System.out.println("3 读取后 = " + Arrays.toString(bArr)+" length = "+length+" , "+new String(bArr));
        length = is.read(bArr);
        System.out.println("4 读取后 = " + Arrays.toString(bArr)+" length = "+length+" , "+new String(bArr,0,length));
        length = is.read(bArr);
        System.out.println("5 length = " + length);
        //3.关闭资源
        is.close();

    }


    @Test
    public void test03() throws Exception{
        //1.创建流对象
        InputStream is = new FileInputStream("D:/a.txt");
        //2.读取数据
        int value = 0;
        while ((value=is.read())!=-1){
            System.out.println("value = " + value+" , "+(char)value);
        }


        //3.关闭资源
        is.close();

    }


    @Test
    public void test02() throws IOException {
        //1.创建流对象
        InputStream is = new FileInputStream("D:/a.txt");
        //2.读取数据
        int value = is.read();
        while (value!=-1){
            System.out.println(value+" , "+(char)value);
            //再读一次
            value=is.read();
        }


        //3.关闭资源
        is.close();

    }


    @Test
    public void test01() throws IOException {
        //1.创建流对象
        InputStream is = new FileInputStream("D:/a.txt");

        //2.读取数据并展示
        int value = is.read();
        System.out.println("1 value = " + value+","+(char)value);
        value = is.read();
        System.out.println("2 value = " + value+","+(char)value);
        value = is.read();
        System.out.println("3 value = " + value+","+(char)value);
        value = is.read();
        System.out.println("4 value = " + value);
        //3.关闭资源
        is.close();

    }
}
